<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\api\logic;


use app\common\model\Company;
use app\common\model\zbtask\Order;
use app\common\model\zbtask\Zbtask;
use think\Db;
use think\exception\ValidateException;

class TaskLogic
{

    private static $new;
    private $task;
    private $where = [
        'shutdownswitch'=> 1,
        'type' => 1,
        'status' => 1,
    ];
    private $order;
    private function __construct() {
        $this->task = new Zbtask();
        $this->order = new Order();
    }



    public static function instance () { //声明一个静态公共方法，供外部获取唯一实例
        if (!(self::$new instanceof self)) {
            self::$new = new self;
        }
        return self::$new;
    }

    /**
     * 设置查询用户
     * @param $where
     * @return  object $this
     */
    public function where($where = []) {
        //设置where条件
        $this->where = array_merge($this->where, $where);
        return $this;
    }

    /**获得单条数据*/
    public function getSingle($task_id) {
        $task = $this->task->get($task_id);
        if (!$task_id) {
            throw new ValidateException('任务不存在');
        }

        $task['company'] = Company::get($task['company_id'])->visible(['id', 'name', 'concat_phone']);
        return $task;
    }

    /**判断是否已经接单该任务*/
    public function isReceive($user_id, $task_id) {
        $where['user_id'] = $user_id;
        $where['task_id'] = $task_id;
        $where['status'] = 1;
        $order = $this->order->where($where)->find();
        return $order ?? 0;
    }
    /**首页专用方法*/
    public function getTask($limit = 10, $where = []) {
        $list = $this->task->where($this->where)
            ->where($where)->limit($limit)
            ->order('createtime desc')->select();

        foreach ($list as $k => $v) {
            $v->company_name = Db::name('company')->where(['id'=> $v->company_id])->value('name');
        }

        return $list;
    }

    /**
     * 根据类别获取任务
     */
     public function taskPages($where = []) {
         $list = $this->task->where($this->where)
             ->where($where)
             ->order('createtime desc')
             ->paginate()
             ->each(function ($item, $key) {
                 $item->company_name = Db::name('company')->where(['id'=> $item->company_id])->value('name');
             });

         return $list;
     }

    /**
     * 创建订单
     */
    public function createOrder($user_id, $task_id) {
        $task = Zbtask::get($task_id);
        if (!$task) {
            throw new ValidateException('任务不正确');
        }
        //不能重复接任务
        $order = $this->isReceive($user_id, $task_id);
        if ($order) {
            throw new ValidateException('不能重复接取任务');
        }

        $data['user_id'] = $user_id;
        $data['task_id'] = $task_id;
        $data['createtime'] = time();
        $data['paytime'] = $task['createtime'];
        $data['company_id'] = $task['company_id'];
        $order = $this->order->create($data);

        if (!$order) {
            throw new ValidateException('创建订单失败');
        }
        return $order;
    }
    private function __clone() {} //声明私有的克隆方法，防止对象被克隆

    public function __sleep() { //重写__sleep方法，将返回置空，防止序列化反序列化获得新的对象
        return [];
    }

}